#ifndef COMSETDIALOG_H
#define COMSETDIALOG_H

#include <QDialog>
#include <QMouseEvent>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTimer>
#include <QDateTime>
#include <QVector>
#include "mainwindow.h"
#include "utils.h"
#include "commandworker.h"
#include <QMouseEvent>
#include <QMoveEvent>
#include "dialogbase.h"

using namespace std;
class MainWindow;
namespace Ui {
class ComSetDialog;
}

class ComSetDialog : public DialogBase
{
    Q_OBJECT

public:
    explicit ComSetDialog(MainWindow* m,QWidget *parent = nullptr);
    ~ComSetDialog();

public:
    //DI3 DI2 DI1 DI0
    QByteArray Frame_Header = QByteArray::fromRawData("\x68\xAA\xAA\xAA\xAA\xAA\xAA\x68\x00\x00", 10);
    QByteArray DI_InFactory=QByteArray::fromHex("0F55FF");
    QByteArray DI_OutFatory=QByteArray::fromHex("0FAAFF");
    QByteArray DI_InitMeter=QByteArray::fromHex("13145480");
    QByteArray DI_MeterNumber=QByteArray::fromHex("02040004");
    QByteArray DI_FactoryVersion=QByteArray::fromHex("01008004");
    QByteArray DI_ChangePwd=QByteArray::fromHex("0C0004");

signals:
    void sendLogByComSetDialog(const QString& text);

    void setWindowTittle(QString tittle);

    void ComSetDialogClosed();

public:
    QSerialPort::BaudRate baudRates[5]={QSerialPort::Baud1200,QSerialPort::Baud2400,QSerialPort::Baud4800,
                                        QSerialPort::Baud9600,QSerialPort::Baud115200};
    QSerialPort::DataBits dataBits[3]={QSerialPort::Data6,QSerialPort::Data7,QSerialPort::Data8};
    QSerialPort::StopBits stopBits[2]={QSerialPort::OneStop,QSerialPort::TwoStop};
    QSerialPort::Parity parityBits[3]={QSerialPort::NoParity,QSerialPort::EvenParity,QSerialPort::OddParity};
    int16_t timeout_ms[15]={100,200,500,800,1000,1500,2000,3000,4000,5000,6000,7000,8000,9000,10000};
    QString pwd_level[4]={"00","02","03","04"};
    template<typename T, size_t N>
    int findIndex(const T (&arr)[N], const T& element) {
        for (size_t i = 0; i < N; ++i) {
            if (arr[i] == element) {
                return i;
            }
        }
        return -1; // 如果没有找到元素，返回-1
    }

    quint8 calculateChecksum(QByteArray arr);
    QByteArray add33(QByteArray arr);

    static bool isFirst;
protected:

private:
    void loadSerialPorts(); //加载串口端口

private slots:

    void on_combox_SerialPort_activated(const QString &arg1);

    void on_Btn_ReadAddress_clicked();

    void on_Btn_WrtieAddress_clicked();

    void on_Btn_CommTest_clicked();

    void ParseData(QByteArray arr);

    void loadSerialPortParams();

    void on_Btn_InFactory_clicked();

    void on_Btn_OutFactory_clicked();

    void on_Btn_InitMeter_clicked();

    void protNameChanged(const QString &arg1);

    void on_combox_BaudRate_currentIndexChanged(int index);

    void on_combox_CheckBit_currentIndexChanged(int index);

    void on_combox_StopBit_currentIndexChanged(int index);

    void on_lineEdit_Password_textChanged(const QString &arg1);

    void on_combox_DataBit_currentIndexChanged(int index);

    void on_combox_PsdLevel_currentIndexChanged(const QString &arg1);

    void on_combox_Timeout_currentIndexChanged(const QString &arg1);

    void on_lineEdit_Address_textChanged(const QString &arg1);


private:


private:
    Ui::ComSetDialog *ui;
    MainWindow* m_mainWindow;
    QString m_PortName;
    QSerialPort::BaudRate m_BuadRate;
    QSerialPort::DataBits m_DataBit;
    QSerialPort::StopBits m_StopBit;
    QSerialPort::Parity m_ParityBits;
    int16_t m_TimeOutMs;

    QList<QByteArray>m_commands;
    QList<QString>m_ports;
};


#endif // COMSETDIALOG_H
